<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="用户id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['commentatorId', validatorRules.commHeadimage]" :disabled="true"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="评论内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['commentContent', validatorRules.commentContent]" :disabled="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="评论时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :disabled="true" v-decorator="['createTime', validatorRules.commentTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否置顶" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group buttonStyle="solid" v-decorator="[ 'isTop', {'initialValue':0}]" @change="myTop">
                <a-radio-button :value="1">置顶</a-radio-button>
                <a-radio-button :value="0">取消</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <div hidden>
            <a-form-item >
              <a-input v-decorator="['id', validatorRules.commHeadimage]" :disabled="true"/>
            </a-form-item>
            <a-form-item >
              <a-input v-decorator="['status', validatorRules.commHeadimage]" :disabled="true"/>
            </a-form-item>
          </div>
          <a-divider />
          <div v-show="commentStatus==0">
            <a-col :span="24" style="text-align: center">
              <a-button @click="publish" type="primary" style="margin-right: 5%">发 布</a-button>
              <a-button @click="myDelete">删 除</a-button>
            </a-col>
          </div>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction,putAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import { postAction } from '@api/manage'

  export default {
    name: 'PhilCommentForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        commentStatus:0,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          commHeadimage: {
            rules: [
              { required: true, message: '请输入头像!'},
            ]
          },
          commentContent: {
            rules: [
              { required: true, message: '请输入评论内容!'},
            ]
          },
          commentTime: {
            rules: [
              { required: true, message: '请输入评论时间!'},
            ]
          },
        },
        url: {
          add: "/phil/philComment/add",
          edit: "/philComment/edit",
          queryById: "/phil/philComment/queryById"
        },
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
  mounted() {
  },
    methods: {
      myTop(e){
        let status = this.form.getFieldValue("status")
        if (status === 1){
          this.$message.error('该条评论已被删除，无法修改！');
          return
        }

        console.log("修改值  ",e.target.value)
        let that = this
        let param  = {
          id : that.form.getFieldValue("id"),
          isTop : e.target.value
        }
        putAction(this.url.edit,param).then(res=>{
          if (res.success){
            this.$message.success('置顶成功！');
          }else {
            this.$message.error('置顶失败！');
          }
        })
      },
      // 删除
      myDelete(){
        let flag = this.form.getFieldValue("status")
        if (flag === 1){
          this.$message.warn('该评论已被删除！');
          return
        }
        // if (flag === 2){
        //   this.$message.warn('该评论已被发布！');
        //   return
        // }
        let that = this
        let param  = {
          id: that.form.getFieldValue("id"),
          status:1
        }
        putAction(this.url.edit,param).then(res=>{
          if (res.success){
            this.$message.success('删除评论成功！');
          }else {
            this.$message.error('删除评论失败！');
          }
          that.$emit("lod")
        })
      },
      // 发布
      publish(){
        let flag = this.form.getFieldValue("status")
        if (flag === 1){
          this.$message.warn('该评论已被删除！');
          return
        }
        if (flag === 2){
          this.$message.warn('该评论已被发布！');
          return
        }
        let that = this
        let param  = {
          id: that.form.getFieldValue("id"),
          status:2
        }
        putAction(this.url.edit,param).then(res=>{
          if (res.success){
            this.$message.success('发布评论成功！');
          }else {
            this.$message.error('发布评论失败！');
          }
          that.$emit("lod")
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'id','commentatorId','commentContent','createTime','isTop','likeNum',"status"))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'commentatorId','commentContent','createTime','isTop','likeNum','status','id'))
      },
    }
  }
</script>